<?php
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Response as FacadesResponse;
/**
 * 用户可以点赞post, comment或者user
 *
 * @author hongyang
 */
class LikeController extends CenterController
{

    protected $layout = 'layouts.master';

    /**
     * 点赞
     */
    public function like()
    {
        $posts = Input::all();
        $likeType = 0;
        if (isset($posts['item_type'])) {
            switch ($posts['item_type']) {
                case 'post':
                    $rules = array(
                        'item_id' => 'required|exists:posts,id'
                    );
                    $likeType = Like::TYPE_POST;
                    break;
                case 'comment':
                    $rules = array(
                        'item_id' => 'required|exists:comments,id'
                    );
                    $likeType = Like::TYPE_COMMENT;
                    break;
                case 'user':
                    $rules = array(
                        'item_id' => 'required|exists:user,id'
                    );
                    $likeType = Like::TYPE_USER;
                    break;
                case 'article':
                    $rules = array(
                        'item_id' => 'required|exists:articles,id'
                    );
                    $likeType = Like::TYPE_ARTICLE;
                    break;
                    
            }
            $messages = array('item_id.exists' => sprintf('点赞的%s不存在', $posts['item_type']));
            $messages['item_id.required'] = sprintf('请提交%s的id', $posts['item_type']);

            Validator::extend ( 'duplicate', function ($attr, $value, $parameters) use ($posts, $likeType) {
                $value = Like::likeExists($likeType, $posts['item_id'], $this->user->id);
                return !$value;
            } );
        }
        $rules['item_type'] = array('required','regex:/^(post|user|comment|article)$/i', 'duplicate');
        $messages['item_type.required'] = '请提交关注类型';
        $messages['item_type.regex'] = '提交的关注类型格式不正确';
        $messages['item_type.duplicate'] = '你已经点过赞了';        
        $validator = Validator::make ( $posts, $rules, $messages );
        if ($validator->fails()) {
			return $this->makeView(
						'errors.400',
						$validator->errors(),
						400
					);
        }       
        // 验证成功
        $userId = $this->user->id;
        $itemId = $posts['item_id'];
        $currentLikesCount = 0;
        try {
            $user = null;
            switch ($posts['item_type']) {
                case 'post':
                    if ($post = Like::likePost($itemId, $userId)) {
                        $user = User::find($post->creator_id);
                        $currentLikesCount = $post->postExtra()->first()->likes_count;
                    }
                    break;
                case 'comment':
                    if ($comment = Like::likeComment($itemId, $userId)) {
                        $user = User::find($comment->creator_id);
                        $currentLikesCount = $comment->commentExtra()->first()->likes_count;
                    }
                    break;
                case 'user':
                    if ($likeUser = Like::likeUser($itemId, $userId)) {
                        $user = $likeUser;
                        $currentLikesCount = $likeUser->likes_count;
                    }
                    break;
                case 'article':
                    if ($article = Like::likeArticle($itemId, $userId)) {
                        $user = User::find($article->creator_id);
                        $currentLikesCount = $article->likes;
                    }
                    break;                    
                    
            }
            if ($user) {
                UserScoresHistory::like($user);
            }
           Event::fire('like', [$user,$currentLikesCount]);
        } catch (Exception $ex) {
            Log::error($ex);
			return $this->makeView(
				'errors.503',
				'服务器暂时没有响应，请稍后再试',
				503	
			);           
        }
        
        return $this->makeView(
            'general.201',
            array("likes_count" =>$currentLikesCount),
            201
        );        
    }
}
